{include file='public/header' /}

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">货&emsp;&emsp;号：</label>
                        <div class="layui-input-inline">
                            <input name="goods_no" value="" class="layui-input" type="text" placeholder="输入货号"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">商&nbsp;&nbsp;品&nbsp;&nbsp;名：</label>
                        <div class="layui-input-inline">
                            <input name="goods_name" value="" class="layui-input" type="text" placeholder="输入商品名"/>
                        </div>
                    </div>

                    <div class="layui-inline" style="padding-left: 20px;">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchTbAdv" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>


                    </div>
                </div>
            </div>
            <table id="tableTbAdv" lay-filter="tableTbAdv"></table>
            <script type="text/html" id="thumb">

                {{# if(d.thumb){ }}
                <a href="{{d.thumb}}" target="_blank" title="点击查看">
                    <img src="{{d.thumb}}" alt="" width="28" height="28">
                </a>
                {{#  } }}
            </script>


            <script type="text/html" id="category">
                {{ d.category.name }}
            </script>
            <script type="text/html" id="tableStatusUser">
                <input type="checkbox" lay-filter="ckStatusUser" value="@{{d.id}}" lay-skin="switch"
                       lay-text="上架|下架" @{{d.goods_status==1?'checked':''}}/>
            </script>
            <script type="text/html" id="options">
                <div class="layui-btn-group">

                    <a class="layui-btn layui-btn-sm" lay-event="edit">选择</a>

                </div>
            </script>
        </div>
    </div>

</div>

{include file='public/footer' /}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;

        form.render('select');

        // 渲染表格
        var insTb = table.render({
            elem: '#tableTbAdv',
            url: "{:url('mall.item/getData',array('market_type'=>1))}",
            page: true
            ,height: 600,
            cols: [[

                {field: 'title', align: 'center', sort: true, title: '商品名称'},
                {field: 'thumb', title: '主图',align: 'center', toolbar: '#thumb'},
                {field: 'goods_no', align: 'center', sort: true, title: '货号'},

                {
                    field: 'create_time', title: '创建时间', sort: true, align: 'center'
                }


                , {fixed: 'right', width: 120, align: 'center', title: '操作',toolbar: '#options'}
            ]]
        });
        //监听工具条
        table.on('tool(tableTbAdv)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'edit') {
                $.post("{:url('market.seckill/choice_item') }",{id:data.id},function (result) {
                    if (result.code==200){

                        //填充数据
                        parent.$("input[name='goods_no']").val(result.data.info.goods_no);
                        parent.$("input[name='item_id']").val(result.data.info.id);
                        parent.$("input[name='store_id']").val(result.data.info.store_id);
                        parent.$("input[name='goods_name']").val(result.data.info.goods_name);
                        parent.$("input[name='market_title']").val(result.data.info.title);
                        parent.$("input[name='goods_price']").val(result.data.info.goods_price);
                        parent.$("input[name='line_price']").val(result.data.info.line_price);
                        parent.$("input[name='inventory']").val(result.data.info.inventory);
                        parent.$("select[name='category_id']").val(result.data.info.category_id);
                        parent.$("checkbox[name='spec_type']").val(result.data.info.spec_type);
                        parent.$("input[name='item_id']").val(result.data.info.id);
                        parent.$('.layui-upload-box').html('<li><img style="width:100px;height:60px" src="'+result.data.info.thumb+'" /></li>');
                        parent.$('.layui-upload-input').val(result.data.info.thumb);
                        //轮播图
                        var image='';
                        $.each(result.data.info.image,function(i,item){
                            image +='<li class="item_img"><div class="operate"></i><i  class="close layui-icon"></i>' +
                                    '</div><img src="' + item + '" class="layui-upload-img" width="60">' +
                                    '<input type="hidden" name="image[]" value="' + item + '"></li>';
                        })
                        parent.$('#demo2').html(image)
                        //规格
                        parent.$('#tbody').html('');
                        if(result.data.info.spec_type == 2){

                            parent.$('#th').val('');
                            var text ='';
                            $.each(result.data.list,function(k,v){
                                text+= '<th>'+v[0].category.spec_name+'</th>';

                            })
                            text+='<th>实际售价</th><th>图片</th><th>全部库存</th><th>活动价</th><th>活动库存</th>';
                            parent.$('#th').html(text);

                            $.each(result.data.info.ItemSku,function(i,item){
                                var html ='<tr>';
                                $.each(item['spec'],function(key,val){
                                    html+=' <th>'+val+'</th>';
                                })
                                html+=  '<td><input type="number" readonly class="goods_price" name="spec['+item.spec_sku_id+'][goods_price]" lay-verify="required" placeholder="请输入商品单价" value="'+item.goods_price +'" class="layui-input"/></td>'+
                                        '<td>'+
                                        "<input type='file'   value='选择图片'>"+
                                        "<input type='button' class='layui-btn' onclick='asd(this)' value='确认上传'>"+
                                        '<input type="hidden" name="spec['+item.spec_sku_id+'][sku_image]" class="layui-upload-input" value="'+item.sku_image +'">'+
                                        '<input type="hidden" name="spec['+item.spec_sku_id+'][sku_id]" class="layui-upload-input" value="'+item.id +'">'+
                                        '<input type="hidden" name="spec['+item.spec_sku_id+'][spec_sku_id]" class="layui-upload-input" value="'+item.spec_sku_id +'">'+
                                        "<img width='50' height='50' style='margin-left: 10px' src='"+item.sku_image +"'>"+
                                        '</td>'+
                                        '<td><input type="number" readonly class="inventory"  lay-verify="required" placeholder="请输入商品库存" value="'+item.inventory +'" class="layui-input"/></td>'+
                                        '<td><input type="number"  class="market_price1" name="spec['+item.spec_sku_id+'][market_price]" lay-verify="required" placeholder="请输入商品活动价" value="" class="layui-input"/></td>'+
                                        '<td><input type="number"  class="inventory1" max="'+item.inventory +'" name="spec['+item.spec_sku_id+'][inventory]" lay-verify="required" placeholder="请输入活动库存" class="layui-input"/></td>'+
                                        '</tr>';

                                parent.$("#tbody").append(html);
                            })
                        }

                        layer.closeAll();
                    }else{
                        layer.msg(result.msg);
                    }




                });
            }
        });

        // 搜索
        form.on('submit(formSubSearchTbAdv)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });


        // 渲染laydate
        laydate.render({
            elem: '#edtDateTbAdv'
        });

    });
</script>